import * as types from '../types'
import {login} from "../../api/login";
import {testLogin} from "../../api/login";
import {setToken, getParentId, getMetting} from "../../util/auth";
import chatHandler from '../../handler/ChatHandler'

export default {
    async login({commit,dispatch}, code) {
        let res = await  login(code)
        let loginToken = res.data.token
        let userId = res.data.userId
        let name = res.extra.wxUser.nickName
        let avatar = res.extra.wxUser.headImgUrl
        //存储到loginToken到Cookies
        setToken(loginToken)

        commit(types.LOGIN, {userId, loginToken, name, avatar})
        commit(types.UPDATE_USER_INFO_MAP,[{
            userId:{name:name,avatar:avatar}
        }])
        //初始化
        await chatHandler.initApp()
    },
    async testLogin({commit}) {
        let res = await  testLogin(36)

        let loginToken = res.data.token
        let userId = res.data.userId
        let name = res.extra.wxUser.nickName
        let avatar = res.extra.wxUser.headImgUrl
        //存储到loginToken到Cookies
        setToken(loginToken)

        commit(types.LOGIN, {userId, loginToken, name, avatar})
        //初始化
        await chatHandler.initApp()
    }
}
